package flc.ast.widget.time;

import android.annotation.SuppressLint;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.graphics.Color;
import android.widget.RemoteViews;

import com.blankj.utilcode.util.TimeUtils;

import java.text.SimpleDateFormat;

import flc.ast.R;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.utils.BitmapUtil;

public class Time7Widget extends BaseTimeWidget {


    public Time7Widget(int appWidgetId) {
        super(appWidgetId);
    }

    @Override
    protected void onUpdate(Context context) {
        @SuppressLint("RemoteViewLayout") RemoteViews remoteViews =
                new RemoteViews(context.getPackageName(), R.layout.widget_time7);
        //准备数据
        String date = TimeUtils.getNowString(new SimpleDateFormat("MM月dd日"));
        String hour = TimeUtils.getNowString(new SimpleDateFormat("HH"));
        String minute = TimeUtils.getNowString(new SimpleDateFormat("mm"));
        String second = TimeUtils.getNowString(new SimpleDateFormat("ss"));
        //更新显示数据
        String time7Color = AppSpUtils.getTime7Color();
        remoteViews.setTextViewText(R.id.tvTime7Date, date);
        remoteViews.setImageViewBitmap(R.id.tvTime7TimeH,
                BitmapUtil.text2BmpWithAssetFont(context, hour,
                        Color.parseColor(time7Color), 28, "font/ziti4.otf"));
        remoteViews.setImageViewBitmap(R.id.tvTime7TimeM,
                BitmapUtil.text2BmpWithAssetFont(context, minute,
                        Color.parseColor(time7Color), 28, "font/ziti4.otf"));
        remoteViews.setImageViewBitmap(R.id.tvTime7TimeS,
                BitmapUtil.text2BmpWithAssetFont(context, second,
                        Color.parseColor(time7Color), 28, "font/ziti4.otf"));
        remoteViews.setImageViewBitmap(R.id.tvTime7Dot1,
                BitmapUtil.text2BmpWithAssetFont(context, ":",
                        Color.parseColor(time7Color), 28, "font/ziti4.otf"));
        remoteViews.setImageViewBitmap(R.id.tvTime7Dot2,
                BitmapUtil.text2BmpWithAssetFont(context, ":",
                        Color.parseColor(time7Color), 28, "font/ziti4.otf"));
        remoteViews.setTextColor(R.id.tvTime7Week1, Color.parseColor("#20333333"));
        remoteViews.setTextColor(R.id.tvTime7Week2, Color.parseColor("#20333333"));
        remoteViews.setTextColor(R.id.tvTime7Week3, Color.parseColor("#20333333"));
        remoteViews.setTextColor(R.id.tvTime7Week4, Color.parseColor("#20333333"));
        remoteViews.setTextColor(R.id.tvTime7Week5, Color.parseColor("#20333333"));
        remoteViews.setTextColor(R.id.tvTime7Week6, Color.parseColor("#20333333"));
        remoteViews.setTextColor(R.id.tvTime7Week7, Color.parseColor("#20333333"));
        remoteViews.setTextColor(getWeekView(), Color.BLACK);

        remoteViews.setInt(R.id.llTime7Bg, "setBackgroundColor", AppSpUtils.getTime7Bg());

        AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, remoteViews);
    }

    //获取需要变色的控件id
    private int getWeekView() {
        int weekId = R.id.tvTime7Week1;
        String week = TimeUtils.getChineseWeek(TimeUtils.getNowString());
        if (week.equals("周一")) {
            weekId = R.id.tvTime7Week1;
        } else if (week.equals("周二")) {
            weekId = R.id.tvTime7Week2;
        } else if (week.equals("周三")) {
            weekId = R.id.tvTime7Week3;
        } else if (week.equals("周四")) {
            weekId = R.id.tvTime7Week4;
        } else if (week.equals("周五")) {
            weekId = R.id.tvTime7Week5;
        } else if (week.equals("周六")) {
            weekId = R.id.tvTime7Week6;
        } else if (week.equals("周日")) {
            weekId = R.id.tvTime7Week7;
        }
        return weekId;
    }
}